<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/sms.jsp"%>
<%@ include file="/common/user.jsp"%>
<%@ include file="/common/message.jsp"%>
<%!
	// 根据手机验证码绑定手机号
	public Message bindPhone(String code, Integer uid, String phone, Integer hospital) {
		Message msg = validateSms(phone, code, BIND_PHONE_SMS, hospital);
		if (msg.getErrcode() != 0)
			return msg;
		User user = userByUid(uid);
		if (user == null)
			return new Message(30041, "没有该用户");
		List<User> list = QBuilder.db("basedb").select().from("User as u").where(new Where("u.status=", STATUS_ENABLE).and("u.hospital=", hospital).and("(u.userName = '" + phone + "' or u.phone='" + phone + "')").and("u.type=", user.getType()).and("u.id!=", user.getId())).page(1, 1).query().getResultObj();
		if (!isEmpty(list))
			return new Message(30031, "该手机号已经绑定了其他账户");
		user.setPhone(phone);
		DBTools.db("basedb").modItem(user, uid);
		UserProfile up = selfProfileByUid(uid);
		if (up != null) {
			up.setPhone(phone);
			DBTools.db("basedb").modItem(up, up.getId());
		}
		return new Message(0, "手机号绑定成功，下次直接用手机号登录");
	}
%>
<%
	Integer uid = token.getUid();
	String phone = input.getDesc("phone", "");
	String code = input.getDesc("code");
	msg = bindPhone(code, uid, phone, hospital);
	Tools.encryptPrint(out, msg);
%>
